package com.redbox.RedboxManagment.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.redbox.RedboxManagment.ejb.Client;

@Repository(value = "clientDao")
public class ClientImp extends HibernateDaoSupport implements IClient {

	@Override
	@Transactional(readOnly = true)
	public Client findById(int pId) {
		String queryString= "from Client a where a.idClient=?";
		List<Client> result = getHibernateTemplate().find(queryString,pId);	
		
	    if (result.size() > 0) {
	        return result.get(0);
	    } else {
	        return null;
	    }   
	}
	
	@Override
	@Transactional(readOnly = true)
	public List<Client> findAll(){
		String queryString= "from Client a";
		List<Client> result = getHibernateTemplate().find(queryString);
		
		if (result.size() > 0) {
            return result;
        } else {
            return null;
        }   
	}

	@Override
	@Transactional
	public Client persistOnMerge(Client client) {
		Client result = (Client) getHibernateTemplate().save(client);
		return result;
	}

}
